import {
  reqGoodsList
} from '../../api/index'
Page({
  data: {
    page: 1,
    limit: 10, //每页商品个数
    category2Id: '',
    goodsArr: [], //存储商品的数组
    flag: '' //more 更多  noMore 没了
  },
  onLoad(options) {
    //存储来自于分类的ID
    options.category2Id && this.setData({
      category2Id: options.category2Id
    });
    this.getGoodsList();
  },
  async getGoodsList() {
    const {
      page,
      limit,
      category2Id
    } = this.data;
    const result = await reqGoodsList(page, limit, category2Id);
    if (result.data.records.length < 10) {
      this.setData({
        flag: 'nomore'
      })
    } else {
      this.setData({
        flag: 'more'
      })
    }
    const goods = this.data.goodsArr;
    goods.push(...result.data.records)
    this.setData({
      goodsArr: goods
    })
  },
  //页面配置项触底
  onReachBottom() {
    //触底的判断
    if (this.data.flag == 'more') {
      //获取下一页
      this.setData({
        page: this.data.page + 1
      });
      //获取数据
      this.getGoodsList();
    }
  }
})